/**
 * kl yy-ds @2021
 */
package com.cxps.server.thread.service;

import com.cxps.cloud.utils.IdUtils;
import com.cxps.server.product.domain.ProductEntity;
import com.cxps.server.thread.mapper.ThreadProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * TODO(类描述)
 *
 * @author YouCai.Liu
 * @since 2021/11/30
 * ...
 */
@Slf4j
@Service
public class ThreadProductService {

    @Autowired
    private ThreadProductMapper threadProductMapper;

    /**
     * 单线程插入数据
     */
    public void oneThread() {
        // ===V1 136S
//        long start = System.currentTimeMillis();
//        for (int i = 0; i < 100000; i++) {
//            ProductEntity entity = new ProductEntity();
//            entity.setId(IdUtils.generateId());
//            entity.setProductName("这是单线程插入的商品数据" + i);
//            entity.setProductTitle("这是单线程插入的商品标题数据" + i);
//            entity.setProductCode(IdUtils.generateId());
//            entity.setProductType(IdUtils.generateId());
//            entity.setChannel(i);
//            entity.setSaleType(i);
//            entity.setUnit("包斤袋");
//            entity.setProductImg(IdUtils.generateId() + ".png");
//            entity.setProductPrice(Double.parseDouble(i + ""));
//            entity.setSaleNum(i);
//            entity.preInsert();
//            entity.setIsUp(1);
//            threadProductMapper.insert(entity);
//        }
//        long end = System.currentTimeMillis();
//        log.info("插入10W数据共耗时：" + (end - start) / 1000 + " S");

        // ===V2 17S
        long start2 = System.currentTimeMillis();
        List<ProductEntity> list = new ArrayList<>();
        int size = 100200;
        for (int i = 0; i < size; i++) {
            ProductEntity entity = new ProductEntity();
            entity.setId(IdUtils.generateId());
            entity.setProductName("这是单线程插入的商品数据" + i);
            entity.setProductTitle("这是单线程插入的商品标题数据" + i);
            entity.setProductCode(IdUtils.generateId());
            entity.setProductType(IdUtils.generateId());
            entity.setChannel(i);
            entity.setSaleType(i);
            entity.setUnit("包斤袋");
            entity.setProductImg(IdUtils.generateId() + ".png");
            entity.setProductPrice(Double.parseDouble(i + ""));
            entity.setSaleNum(i);
            entity.preInsert();
            entity.setIsUp(1);
            if (list.size() == 500) {
                threadProductMapper.batchInsert(list);
                list.clear();
            } else {
                list.add(entity);
            }
        }
        if (list.size() > 0) {
            threadProductMapper.batchInsert(list);
            list.clear();
        }
        long end2 = System.currentTimeMillis();
        log.info("插入10W数据共耗时：" + (end2 - start2) / 1000 + " S");
    }

    /**
     * 多线程插入数据
     */
    public void moreThread() {

        // ===多线程V1 27S
        // ===多线程V2 8S

        long start2 = System.currentTimeMillis();
        int size = 100200;
        int threadSize = size / 10;
        Vector<Thread> threads = new Vector<>();
        for (int i = 0; i < 10; i++) {
            ThreadService service = new ThreadService();
            service.setThreadSize(threadSize);
            service.setThreadProductMapper(threadProductMapper);
            service.setName("myThread" + i);
            threads.add(service);
            service.start();
        }

        for (Thread iThread : threads) {
            try {
                iThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        long end2 = System.currentTimeMillis();
        log.info("插入10W数据共耗时：" + (end2 - start2) / 1000 + " S");
    }

    /**
     * moreThread2
     */
    public void moreThread2() {

        // ===多线程V1 27S
        // ===多线程V2 8S

        long start2 = System.currentTimeMillis();

        int size = 100200;
        int threadSize = size / 10;

        // 创建服务类对象
        ThreadService2 threadService2 = new ThreadService2();
        threadService2.setThreadSize(threadSize);
        threadService2.setThreadProductMapper(threadProductMapper);

        // 线程队列
        Vector<Thread> threads = new Vector<>();
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(() ->{
                threadService2.run();
            }, "myThread" + i);
            threads.add(t);
        }

        // 遍历启动线程
        for (Thread thread : threads) {
            thread.start();
        }

        // 遍历线程，让线程等待
        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        long end2 = System.currentTimeMillis();
        log.info("插入10W数据共耗时：" + (end2 - start2) / 1000 + " S");
    }
}
